﻿namespace TriangleGames.TrainResEditor
{
    using TriangleGames.TrainRes;
    using System;
    using UnityEditor;
    using UnityEngine;

    internal static class BezierLink
    {
        public static void Draw(FsmState fromState, FsmState toState, int transitionIndex, Color linkColor, float linkWidth, Texture leftArrow, Texture rightArrow)
        {
            float num7;
            float num8;
            FsmTransition transition = fromState.Transitions[transitionIndex];
            FsmTransition.CustomLinkConstraint linkConstraint = transition.LinkConstraint;
            float y = (fromState.Position.y + ((transitionIndex + 1) * 16f)) + 8f;
            float num2 = toState.Position.y + 8f;
            float num3 = fromState.Position.x - 1f;
            float num4 = (num3 + fromState.Position.width) + 2f;
            float num5 = toState.Position.x - 1f;
            float num6 = (num5 + toState.Position.width) + 2f;
            Vector3 startPosition = new Vector3(0f, y, 0f);
            Vector3 endPosition = new Vector3(0f, num2, 0f);
            switch (linkConstraint)
            {
                case FsmTransition.CustomLinkConstraint.None:
                    if (num6 >= num3)
                    {
                        if (num5 < num3)
                        {
                            startPosition.x = num3;
                            endPosition.x = num5 - leftArrow.width;
                            num7 = -1f;
                            num8 = -1f;
                        }
                        else if (num5 > num4)
                        {
                            startPosition.x = num4;
                            endPosition.x = num5 - leftArrow.width;
                            num7 = 1f;
                            num8 = -1f;
                        }
                        else
                        {
                            startPosition.x = num4;
                            endPosition.x = num6 + leftArrow.width;
                            num7 = 1f;
                            num8 = 1f;
                        }
                        break;
                    }
                    startPosition.x = num3;
                    endPosition.x = num6 + leftArrow.width;
                    num7 = -1f;
                    num8 = 1f;
                    break;

                case FsmTransition.CustomLinkConstraint.LockLeft:
                    if (num6 >= num3)
                    {
                        startPosition.x = num3;
                        endPosition.x = num5 - leftArrow.width;
                        num7 = -1f;
                        num8 = -1f;
                        break;
                    }
                    startPosition.x = num3;
                    endPosition.x = num6 + leftArrow.width;
                    num7 = -1f;
                    num8 = 1f;
                    break;

                default:
                    if (num5 > num4)
                    {
                        startPosition.x = num4;
                        endPosition.x = num5 - leftArrow.width;
                        num7 = 1f;
                        num8 = -1f;
                    }
                    else
                    {
                        startPosition.x = num4;
                        endPosition.x = num6 + leftArrow.width;
                        num7 = 1f;
                        num8 = 1f;
                    }
                    break;
            }
            Vector3 vector5 = startPosition - endPosition;
            float a = vector5.magnitude * 0.5f;
            float num10 = Mathf.Min(a, 40f);
            if (fromState == toState)
            {
                num10 = 50f;
            }
            Vector3 startTangent = startPosition;
            startTangent.x += num7 * num10;
            Vector3 endTangent = endPosition;
            endTangent.x += num8 * num10;
            Handles.DrawBezier(startPosition, endPosition, startTangent, endTangent, linkColor, FsmEditorStyles.LineTexture, linkWidth * 2f);
            endPosition.x -= num8 * leftArrow.width;
            Color color = GUI.color;
            GUI.color = Color.red;
            if (num8 != 1f)
            {
                leftArrow = rightArrow;
                endPosition.x -= leftArrow.width;
            }
            FsmGraphView.DrawArrowHead(leftArrow, endPosition, linkColor, false);
            GUI.color = color;
        }
    }
}

